const app = getApp();
const util = require('../../../utils/util.js');
import grace from "../../../utils/grace.js";

grace.page({
  data: {
    weeks_ch: [],
    allDays: [],
    attendance_days: []
  },
  //==================加载数据================
  onLoad(e) {
    wx.showLoading({
      title: '加载中...',
    })
    // 查询近三个月出勤日期
    let url = app.globalData.api + '/api/v1.0/child/attendance/' + e.student_id;
    this.$http.get(url, {token: wx.getStorageSync('token')}).then((res) => {
      if (res.data.code == 0) {
        this.$data.attendance_days = res.data.data;
      }
      wx.hideLoading();
      this.updateAttendanceDates();
    }).catch(err => {
      console.log(err);
    })
  },
  updateAttendanceDates() {
    let date = new Date();
    //获取当前的年月
    let cur_year = date.getFullYear();
    let cur_month = date.getMonth() + 1;
    let cur_day = date.getDate();
    this.$data.weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    //设置数据
    this.$data = {
      org_year: date.getFullYear(),//现在日期的年月日
      org_month: date.getMonth(),
      org_day: cur_day
    }
    this.refreshData(cur_year, cur_month);
  },
  //刷新数据
  refreshData(cur_year, cur_month) {
    // 计算最近三个月的对象
    let mObject0 = this.calculateDays(cur_year, cur_month);// 本月
    if (cur_month - 1 < 1) {
      cur_year = cur_year - 1;
      cur_month = 12;
    } else {
      cur_month = cur_month - 1;
    }
    let mObject1 = this.calculateDays(cur_year, cur_month);// 上月
    if (cur_month - 1 < 1) {
      cur_year = cur_year - 1;
      cur_month = 12;
    } else {
      cur_month = cur_month - 1;
    }
    let mObject2 = this.calculateDays(cur_year, cur_month);// 上上月
    this.$data.allDays = [mObject0, mObject1, mObject2];
  },
  // =============获取当月有多少天（下个月月初是多少）==========
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  // =============获取当月第一周第一天是周几===========
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  //====================计算当前年月空的几天 =============
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
    }
    return empytGrids;
  },
  // =====================计算当前年月有哪些天===========
  /**
   * 根据年月计算当前月的天对象状态返回对象
   * mObject 年月对象
   * year  年
   * month 月
   * hasEmptyGrid  是都有空调
   * empytGrids  空天数字
   * days 所有日对象
   */
  calculateDays(year, month) {
    let mObject = {};//月对象
    mObject["year"] = year;
    mObject["month"] = month;
    //计算当前年月空的几天
    let empytGrids = this.calculateEmptyGrids(year, month);
    if (empytGrids.length > 0) {
      mObject["hasEmptyGrid"] = true;
      mObject["empytGrids"] = empytGrids;
    } else {
      mObject["hasEmptyGrid"] = false;
      mObject["empytGrids"] = [];
    }
    let days = [];
    let thisMonthDays = this.getThisMonthDays(year, month);//这个月有多少天
    for (let i = 1; i <= thisMonthDays; i++) {
      let day = {};
      //加入的日期
      let date = new Date(year, month - 1, i);
      //status 1-出勤日期
      day["day"] = i;
      day["status"] = 0;// 默认未出勤
      // 设置出勤日期的状态
      for (let j = 0; j < this.$data.attendance_days.length; j++) {
        let itime = parseInt(util.calculateTime(date, util.stringToDate(this.$data.attendance_days[j])));
        if (itime == 0) {
          day["status"] = 1;
        }
      }
      days.push(day);
    }
    mObject["days"] = days;
    return mObject;
  }
})